package com.f8q8.agent.action;

import com.f8q8.agent.utils.AndroidUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Slf4j
@Controller
public class IndexController {

    @GetMapping
    public String index(Model model) {
        // 获取手机udid列表
        List<String> udids = AndroidUtils.findUdids();
        model.addAttribute("udids", udids);
        return "index";
    }

    @PostMapping(value = "click")
    @ResponseBody
    public Object click(String udid, Integer x, Integer y, Integer times, Integer term) {
        for (int i = 0 ; i < times; i++) {
            log.info("第{}次点击", (i + 1));
            try {
                AndroidUtils.click(udid, x, y);
                Thread.sleep(term);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        return "success";
    }

    @PostMapping(value = "sendMsg")
    @ResponseBody
    public Object sendMsg(String content, String udid, Integer msgInputX, Integer msgInputY, Integer msgSendX, Integer msgSendY) {
        try {
            log.info(content);
            log.info("准备点击输入框");
            AndroidUtils.click(udid, msgInputX, msgInputY);
            log.info("输入框点击完毕，准备输入内容");
            Thread.sleep(1200L);
            // 切换输入法
            AndroidUtils.changeIme(udid);
            // 输入文字
            // content = "宇宙，银河系，太阳系，地球，亚洲，中国，江苏，苏州，吴中，木渎，凯马广场156号，华田雪佛兰";
            AndroidUtils.input(udid, content);
            log.info("内容输入完毕，准备点击发送按钮");
            Thread.sleep(300L);
            // 点击发送
            AndroidUtils.click(udid, msgSendX, msgSendY);
            log.info("发送按钮点击完毕，消息发送成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "success";
    }
}
